﻿using System;
using BSCMFF.Web.DomainService;
using System.ServiceModel.DomainServices.Client;
using BSCMFF.Core.DataAccessLayer;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using StrategyMap.UserControls;
using Microsoft.Expression.Interactivity.Layout;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;
using Microsoft.Expression.Controls;
using Microsoft.Expression.Media;
using StrategyMap.BusinessLayer;

namespace StrategyMap
{
    public partial class Map : UserControl
    {
        private void AddNodesIntoStrategyMap()
        {
            foreach (Strategy_Node sn in loadOpNodes.Entities)
            {
                Node1 n = new Node1();
                n.StrategyName.Text = sn.Strategy_Node_ID.ToString();
                n.Name = sn.Strategy_Node_ID.ToString();

                Canvas.SetLeft(n, Convert.ToDouble(sn.CoordX));
                Canvas.SetTop(n, Convert.ToDouble(sn.CoordY));

                n.Radius = (int)sn.Bend;

                Color cb;
                if (sn.ColorBackground == null)
                {
                    cb = Colors.Transparent;
                }
                else
                {
                    cb = ColorTranslator.TranslateFromHex(sn.ColorBackground);
                }
                n.Main.Fill = new SolidColorBrush(cb);

                MouseDragElementBehavior drag = new MouseDragElementBehavior();
                drag.ConstrainToParentBounds = true;
                //Interaction.GetBehaviors(n).Add(drag);
                n.StartDrawingArrowEvent += new MouseButtonEventHandler(Node_StartDrawingArrowEvent);
                //n.EventShowNodeDetails += new MouseButtonEventHandler(n_EventShowNodeDetails);
                n.MouseLeftButtonDown += new MouseButtonEventHandler(n_EventShowNodeDetails);

                LoadNodeDataContext(n, sn);

                LayoutRoot.Children.Add(n);
                nodes1.Add(n);
            }
        }

        private void LoadNodeDataContext(Node1 n, Strategy_Node sn)
        {
            n.DataContext = sn.Strategy_Node_ID; // CoordX;
        }

        void n_EventShowNodeDetails(object sender, MouseButtonEventArgs e)
        {
            //Smal4.Text = "tralal";
            if (EventNodeClicked != null)
            {
                EventNodeClicked(sender, new EventArgs());
            }
        }

        private Point pFrom;
        //private LineArrow la;

        private void CreateTemporaryArrow(MouseButtonEventArgs e)
        {
            pFrom = e.GetPosition(null);

           
            la = new LineArrow();
            la.Stroke = new SolidColorBrush(Colors.Cyan);
            la.StrokeThickness = 3;
            //la.StartArrow = (ArrowType)CornerType.BottomLeft;
            la.BendAmount = 0;
            la.StartArrow = ArrowType.NoArrow;
            Canvas.SetLeft(la, pFrom.X);
            Canvas.SetTop(la, pFrom.Y);
            la.Width = 10;
            la.Height = 10;


            LayoutRoot.Children.Add(la);
        }

        private void AddTargetEventsForNodes()
        {
            List<int> nodesAlreadyTargeted = new List<int>();
            foreach (Strategy_Arrow sa in strategyContext.Strategy_Arrows)
            {
                if (sa.Strategy_Node_From_FK.ToString() == NodeDrawingArrowFrom.Name)
                {
                    nodesAlreadyTargeted.Add(sa.Strategy_Node_To_FK);
                }
            }

            foreach (Node1 n1 in nodes1)
            {
                bool dontAdd = false;
                foreach (int i in nodesAlreadyTargeted)
                {
                    if (n1.Name == i.ToString())
                    {
                        dontAdd = true;
                        break;
                    }
                }

                if (dontAdd == false && n1.Name != NodeDrawingArrowFrom.Name)
                {
                    n1.MouseEnter += new MouseEventHandler(Node_MouseEnter);
                    n1.MouseLeave += new MouseEventHandler(Node_MouseLeave);
                    n1.MouseLeftButtonUp += new MouseButtonEventHandler(Node_MouseLeftButtonUp);
                }

            }
        }
    }
}
